﻿Imports System.Data
Imports System.Data.SqlClient
Partial Class nquicach
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        If Not Page.IsPostBack Then
            BindGrid()
        End If
    End Sub

    Public Sub Datagrid_OnCancel(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
        DataGrid1.EditItemIndex = -1
        BindGrid()
    End Sub
    Public Sub Datagrid_OnEdit(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
        DataGrid1.EditItemIndex = e.Item.ItemIndex
        BindGrid()
    End Sub
    Public Sub Datagrid_OnUpdate(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)

        Dim sSql As String
        Dim blnAddNew As Boolean = False
        Dim txtTenquicachV As TextBox
        Dim txtTenquicachE As TextBox
        Dim txtTenquicachF As TextBox
        Dim txtTenquicachG As TextBox
        Dim txtTenquicachC As TextBox
        Dim txtTenquicachS As TextBox
        Dim txtNhom As TextBox
        Dim txtIDCombQC As TextBox

        '----------------Lay du lieu -------------

        txtTenquicachV = e.Item.Cells(2).Controls(0)
        txtTenquicachE = e.Item.Cells(3).Controls(0)
        txtTenquicachF = e.Item.Cells(4).Controls(0)
        txtTenquicachG = e.Item.Cells(5).Controls(0)
        txtTenquicachS = e.Item.Cells(6).Controls(0)
        txtNhom = e.Item.Cells(7).Controls(0)
        txtIDCombQC = e.Item.Cells(8).Controls(0)

        '----------------Update du lieu -------------
        If e.Item.Cells(1).Text < 1 Then
            blnAddNew = True
        End If
        If blnAddNew = True Then
            ' Insert into
            sSql = " insert into quicach (tenquicachV,tenquicachE,tenquicachF,tenquicachG,tenquicachS,idsanpham,nhom, IDCombQC)" & _
            "values( @tenquicachV,@tenquicachE,@tenquicachF,@tenquicachG,@tenquicachS,@idsanpham,@nhom,@IDCombQC) "
        Else

            'Update

            sSql = " UPDATE quicach SET" & _
            " tenquicachV =@tenquicachV, tenquicachE =@tenquicachE" & _
            " ,tenquicachF =@tenquicachF, tenquicachG =@tenquicachG" & _
            " ,tenquicachS =@tenquicachS" & _
            ",nhom=@nhom" & _
            ",IDCombQC=@IDCombQC" & _
            " WHERE idquicach = @idquicach"
            
        End If
        Dim myConn As New SqlConnection(ConfigurationSettings.AppSettings("cons"))
        Dim MyCommand As SqlCommand
        myConn.Open()
        MyCommand = New SqlCommand(sSql, myConn)

        If blnAddNew Then
            MyCommand.Parameters.Add(New SqlParameter("@idsanpham", Request.QueryString("id")))
        Else
            MyCommand.Parameters.Add(New SqlParameter("@idquicach", e.Item.Cells(1).Text))
        End If
        MyCommand.Parameters.Add(New SqlParameter("@tenquicachV", txtTenquicachV.Text))
        MyCommand.Parameters.Add(New SqlParameter("@tenquicachE", txtTenquicachE.Text))
        MyCommand.Parameters.Add(New SqlParameter("@tenquicachF", txtTenquicachF.Text))
        MyCommand.Parameters.Add(New SqlParameter("@tenquicachG", txtTenquicachG.Text))
        MyCommand.Parameters.Add(New SqlParameter("@tenquicachS", txtTenquicachS.Text))
        If txtNhom.Text <> "" Then
            MyCommand.Parameters.Add(New SqlParameter("@nhom", Left(txtNhom.Text, 6)))
        Else
            MyCommand.Parameters.Add(New SqlParameter("@nhom", ""))
        End If
        MyCommand.Parameters.Add(New SqlParameter("@IDCombQC", txtIDCombQC.Text))
        MyCommand.ExecuteNonQuery()
        myConn.Close()
        DataGrid1.EditItemIndex = -1
        BindGrid()
    End Sub
    Private Sub BindGrid()
        Dim idsanpham As Int16 = -1
        If IsNumeric(Request.QueryString("id")) Then
            idsanpham = Request.QueryString("id")
        End If
        Dim dr As DataRow
        Dim myConn As New SqlConnection(ConfigurationSettings.AppSettings("cons"))
        Dim strWhere As String = ""
        If TextBox1.Text <> "" Then
            strWhere += " and tenquicachV like N'%" & TextBox1.Text & "%'"
        End If
        Dim sSql As String = "select idquicach,  tenquicachV,tenquicachE,tenquicachF,tenquicachG,tenquicachC,tenquicachS,giaV,giaE,giaF,giaC,chuan,nhom, IDCombQC  from quicach where idsanpham=" & idsanpham & strWhere & " order by nhom, giaV"
        myConn.Open()
        Dim myAdapter As New SqlDataAdapter(sSql, myConn)
        Dim myDataSet As New DataSet
        myAdapter.Fill(myDataSet, sSql)
        dr = myDataSet.Tables(0).NewRow()
        dr(0) = -1
        dr(1) = "Thêm mới"
        myDataSet.Tables(0).Rows.InsertAt(dr, 0)
        DataGrid1.DataSource = myDataSet
        DataGrid1.DataBind()
        myConn.Close()
    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        BindGrid()
    End Sub
End Class

